package com.saicmotor.springboot_demo.service.impl;

import com.saicmotor.springboot_demo.mapper.UserMapper;
import com.saicmotor.springboot_demo.model.User;
import com.saicmotor.springboot_demo.config.UserConfig;
import com.saicmotor.springboot_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserConfig userConfig;

    @Override
    public int getAgeByName(String name){
        return userMapper.getAgeByName(name);
    }

    @Override
    public List<String> getNameByAge(int age) {
        return userMapper.getNameByAge(age);
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public void insertUserInfo(String name, int age) {
        userMapper.insertUserInfo(name,age);
    }

    @Override
    public void deleteByName(String name) {
        userMapper.deleteByName(name);
    }

    @Override
    public void deleteByAge(int age) {
        getNameByAge(age).forEach((name)->{
            deleteByName(name);
        });
    }

    @Override
    public void updateAgeByName(String name, int age) {
        userMapper.updateAgeByName(name,age);
    }

    @Override
    public void createUserTable(String tableName) {
        userMapper.createUserTable(tableName);
    }

    @Override
    public void initUserTable() {
        userConfig.getUser().forEach(user -> {
            userMapper.insertUserInfo(user.getName(),user.getAge());
        });
    }
}
